<script setup>
import {ref} from "vue";
import {useForm} from "@inertiajs/vue3";
import useDialog from '@/composables/useDialog'
import useLoading from '@/composables/useLoading'
import useRequest from '@/composables/useRequest'
import {Message} from "@arco-design/web-vue";

const {FormRef, visible, mode, pk, actionName, width} = useDialog()
const {loading, setLoading} = useLoading()

const {createApi, updateApi, showApi} = useRequest('admin.setting.permissions.roles')

const emit = defineEmits(["success", "close"]);

const form = useForm({
    slug: '',
    name: '',
    description: '',
    sort: 50,
    status: 1,
    rules: [],
})

const rules = ref({
    slug: [{required: true, message: '请输入角色标识'}],
    name: [{required: true, message: '请输入角色名称'}],
    description: [{required: true, message: '请输入角色描述'}],
    sort: [{required: true, message: '请输入角色排序'}],
    status: [{required: true, message: '请选择角色状态'}],
    rules: [{required: true, message: '请选择角色权限'}],
})


const handleSubmit = async () => {
    try {
        setLoading(true)
        const res = await FormRef.value?.validate()
        if (res) return false
        const apiMethod = mode.value === 'edit'
            ? () => updateApi(pk.value, form)
            : () => createApi(form)
        await apiMethod()
        Message.success(`${actionName.value}成功`)
        emit('success')
    } catch (error) {
        return false
    } finally {
        setLoading(false)
    }
}

const handleClose = () => {
    FormRef.value?.resetFields()
    form.reset()
    emit('close')
}

// 打开弹窗
const add = async () => {
    mode.value = 'add'
    visible.value = true
}

const edit = async (id) => {
    try {
        setLoading(true)
        pk.value = id
        const {data} = await showApi(id)
        Object.assign(form, data)
        mode.value = 'edit'
        visible.value = true
    } finally {
        setLoading(false)
    }
}

defineExpose({add, edit})
</script>
<template>
    <a-drawer
        v-model:visible="visible"
        :loading="loading"
        :title="`账号${actionName}`"
        :width
        @close="handleClose"
        @before-ok="handleSubmit"
    >
        <a-spin :loading="loading" class="w-full">


            <a-form ref="FormRef" :model="form" :rules auto-label-width>

                <a-form-item
                    field="name"
                    label="名称"
                >
                    <a-input
                        v-model.trim="form.name"
                        :max-length="8"
                        allow-clear
                        placeholder="请输入角色名称"
                        show-word-limit
                    />
                </a-form-item>
                <a-form-item
                    field="slug"
                    label="标识"
                >
                    <a-input
                        v-model.trim="form.slug"
                        :max-length="8"
                        allow-clear
                        placeholder="请输入角色标识"
                        show-word-limit
                    />
                </a-form-item>

                <a-form-item
                    field="description"
                    label="描述"
                >
                    <a-input
                        v-model.trim="form.description"
                        allow-clear
                        placeholder="请输入角色描述"
                    />
                </a-form-item>

                <a-form-item
                    field="status"
                    label="状态"
                >
                    <a-radio-group v-model="form.status">
                        <a-radio :value="1">正常</a-radio>
                        <a-radio :value="0">禁用</a-radio>
                    </a-radio-group>
                </a-form-item>

                <a-form-item field="sort" label="排序">
                    <a-input-number v-model="form.sort" :max="9999" :min="1" :step="10" mode="button"
                                    placeholder="请输入排序"/>
                </a-form-item>

                <a-form-item field="rules" label="权限">
                    <a-tree
                        ref="TreeRef"
                        v-model:checked-keys="form.rules"
                        :check-strictly="false"
                        :data="$page.props.options"
                        :fieldNames="{ key: 'id' }"
                        :show-line="true"
                        checkable
                    ></a-tree>
                </a-form-item>
            </a-form>
        </a-spin>
    </a-drawer>
</template>

<style scoped>

</style>
